import asyncio
from typing import List
import time
async def execute_on(condition: asyncio.Condition,coro,predicate):
    """
    满足条件执行指定协程任务
    :param condition:
    :param coro: 满足条件后执行的协程任务
    :param predicate: 检测条件
    :return:
    """
    async with condition:
        await condition.wait_for(predicate)
        await coro


async def make_tea():
    print("泡茶")


async def process_steps(condition: asyncio.Condition, steps:List[str]):
    while steps:
        print(steps.pop())
        await asyncio.sleep(0.5)
        #没执行一个步骤就通知一次, condition.wait_for 收到通知就会自动执行 谓词函数
        async with condition:
            condition.notify_all()



async def main():
    steps = ["洗水杯","烧水","准备茶叶"]

    def is_ready():
        print("is_ready  执行中...")
        return not steps
    condition = asyncio.Condition()
    await process_steps(condition,steps)
    print(steps)
    await execute_on(condition,make_tea(),is_ready)

async def main_():
    steps = ["洗水杯","烧水","准备茶叶"]
    def is_ready():
        print("is_ready  执行中...")
        return not steps
    condition = asyncio.Condition()
    task1 = asyncio.create_task(process_steps(condition,steps))
    task2 = asyncio.create_task(execute_on(condition,make_tea(),is_ready))

    await asyncio.gather(task1,task2)

if __name__ == '__main__':
    asyncio.run(main_())